<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/logic/LogicHoC.class.php');
require_once(SITE_ROOT . 'lib/Session.php');

	$logic = LogicHoC::getInstance();
	$smarty = new SmartyMaptrick();
	
	if (isset($_POST['select_countries'])) $countryID = $_POST['select_countries'];
	if (isset($_POST['select_divisions'])) $divisionID = $_POST['select_divisions'];
	if (isset($_POST['select_leagues'])) $leagueID = $_POST['select_leagues'];
	
	$hidden = $_POST['hidden'];
	
	if ($hidden == 'login') {
		try {
			$register = new Registration;
			$register->login($_POST['user'], $_POST['password']);
		}
		catch (Exception $e) {
			$smarty->assign('message', $e->getMessage());
		}
	}
	else if ($hidden == 'logout') {
		try {
			$register = new Registration;
			$register->logout();
		}
		catch (Exception $e) {}
	}
	else if ($hidden == 'fetch') {
		if ($leagueID != 0) {
			try {
				$league = $logic->fetchLeague($leagueID);
			}
			catch (Exception $e) {
			$errorCode = substr($e->getMessage(), strpos($e->getMessage(), "(") + 1, strpos($e->getMessage(), ")") - strpos($e->getMessage(), "(") - 1);
			switch ($errorCode) {
				case 0: {
					unset($_SESSION['login']);
					unset($_SESSION['login_teamID']);
					unset($_SESSION['HT']);
					$smarty->assign('message', 'Timeout!');
					break;
				}
				default: $smarty->assign('error', "An error has occured! Please try again later.");
			}			
		}
		}
		else $smarty->assign('error', "You have to choose a league!");
	}
		
	$smarty->assign('action', $_SERVER['PHP_SELF']);
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('home_active', '');
	$smarty->assign('cool_active', '');
	$smarty->assign('hoc_active', 'active');
	
	$smarty->assign('introduction', "<p>You can import a Hattrick league here. You have to be logged in to do anything. To import a league choose the league with the drop down boxes and press \"Fetch\" afterward. You have to select a country first, then the division and at last the league. If everything was successfull a link to the Hall of Cool is presented to you where you can update all the members of the league then.</p>");
	
	$countries = $logic->getCountries();
	$smarty->assign_by_ref('countries', $countries);
	
	if ($leagueID != 0) {
		$league = $logic->getLeague($leagueID);
		if (isset($league)) {
			$smarty->assign('selected_country', $league->getDivision()->getCountry()->getCountryID());
			$smarty->assign_by_ref('divisions', $logic->getDivisions($league->getDivision()->getCountry()->getCountryID()));
			$smarty->assign('selected_division', $league->getDivision()->getDivisionID());
			$smarty->assign_by_ref('leagues', $logic->getLeagues($league->getDivision()->getCountry()->getCountryID(), $league->getDivision()->getDivisionID()));
			$smarty->assign('selected_league', $league->getLeagueID());
		}
	}
	else if ($countryID != 0 && $divisionID != 0) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign_by_ref('divisions', $logic->getDivisions($countryID));
		$smarty->assign('selected_division', $divisionID);
		$smarty->assign_by_ref('leagues', $logic->getLeagues($countryID, $divisionID));
	}
	else if ($countryID  != 0) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign_by_ref('divisions', $logic->getDivisions($countryID));
	}
	
	if ($league != null) {
		
		foreach ($countries as $country) {
			if ($country->getCountryID() == $league->getDivision()->getCountry()->getCountryID()) {
				$selectedCountry = $country;
				break;
			}
		}
		
		$smarty->assign('info', "All teams, except for BOT teams, which play in the league \"" . $league->getName() . "\" in " . $selectedCountry->getName() . " were imported successfully. You can take a look at the league in the Hall of Cool by clicking <a href=\"hallofcool.php?league=" . $league->getLeagueID() . "\">here</a>.");
	}
	
	// display it
	$smarty->display('header.tpl');
	$smarty->display('leagueimport.tpl');	
	$smarty->display('footer.tpl');

?>